using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using System;


    public abstract class PanelRoot
    {

        protected GameObject gameobject;
        public GameObject Game_Object => gameobject;

        protected PanelRoot parent;
        protected List<PanelRoot> children;

        protected bool issuspend;
        public bool Issupend => Issupend;
        protected bool isShowPanelAfterExit;
        protected bool isStart;
        protected bool isEnter;

        public PanelRoot(PanelRoot parent)
        {
            children = new List<PanelRoot>();
            this.parent = parent;
        }

        protected virtual void GameStart()
        {
            Oninit();
        }

        public virtual void GameUpdate()
        {
            if (!isStart)
            {
                isStart = true;
                GameStart();
            }
            foreach (PanelRoot child in children)
            {
                child.GameUpdate();
            }
            if (issuspend == false)
            {
                OnUpdate();
            }
        }
        protected virtual void Oninit()
        {
            OnSuspend();
            if (gameobject == null)
            {
                gameobject = GameTool.Instance.GetGameObjectFromCanvas(GetType().Name);
                if (gameobject == null)
                {
                    gameobject = UnityEngine.Object.Instantiate(ProxyFactory.Instance.resourceFactory.GetPanel(GetType().Name), GameTool.Instance.GetMainCanvas().transform);
                }
                if (gameobject == null)
                {
                    return;
                }
            }
        }
        protected virtual void OnEnter()
        {
            Game_Object.SetActive(true);
            Debug.Log(this);
        }
        protected virtual void OnUpdate()
        {
            if (!isEnter)
            {
                isEnter = true;
                OnEnter();
            }
        }
        protected virtual void OnSuspend()
        {
            issuspend = true;
        }
        protected virtual void OnResume()
        {
            issuspend = false;
        }
        public virtual void OnExit()
        {
            if (!isShowPanelAfterExit)
            {
                gameobject.SetActive(false);
            }
            OnSuspend();
            parent.isEnter = false;
            parent.OnResume();
        }
        protected virtual void EnterPanel(Type type)
        {
            List<PanelRoot> panels = children.Where(x => x.GetType() == type).ToList();
            if (panels.Count == 1)
            {
                panels[0].Game_Object.SetActive(true);
                panels[0].OnResume();
                panels[0].isEnter = false;
                OnSuspend();
            }

        }
        protected T GetPanel<T>() where T : PanelRoot
        {
            return children.Where(p => p is T).ToArray()[0] as T;
        }
    }
